// pages/posts/post-detail/post-detail.js
var postsData = require("../../../data/posts-data.js");
console.log(postsData);
// 获取全局变量
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPlay: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 获取当前点击的id
    var postId = options.id;
    // 获取当前点击的文章页面数据
    var postData = postsData.postList[postId];
    this.setData({
      postData: postData
    })
    // 读取所有文章详情页的收藏情况    
    var postsCollected = wx.getStorageSync("posts_collected");
    console.log(postsCollected);
    // 获取当前点击的文章收藏情况
    // 如果所有文章的收藏情况存在
    if (postsCollected) {
      var postCollected = postsCollected[postId];
      console.log(postCollected);
      // 如果当前点击的文章收藏情况存在，即读取过该文章
      if (postCollected) {
        this.setData({
          collected: postCollected
        })
      } else {
        postsCollected[postId] = false;
        wx.setStorageSync("posts_collected", postsCollected);
      }
    } else {
      postsCollected = {};
      postsCollected[postId] = false;
      wx.setStorageSync("posts_collected", postsCollected);
    }
    this.setMusicMonitor();
    if (app.globalData.g_isPlayingMusic && app.globalData.g_currentMusicPostId == postId) {
      this.setData({
        isPlay: true
      })
    }

  },
  setMusicMonitor: function() {
    // 监听音乐播放事件
    var that = this;
    wx.onBackgroundAudioPlay(function() {
      that.setData({
        isPlay: true
      })
      app.globalData.g_isPlayingMusic = true;
      app.globalData.g_currentMusicPostId = that.data.postData.postId;
    });
    // 监听音乐暂停事件    
    wx.onBackgroundAudioPause(function() {
      that.setData({
        isPlay: false
      })
      app.globalData.g_isPlayingMusic = false;
      app.globalData.g_currentMusicPostId = null;
    });
    // 监听音乐停止事件    
    wx.onBackgroundAudioStop(function() {
      that.setData({
        isPlay: false
      })
      app.globalData.g_isPlayingMusic = false;
      app.globalData.g_currentMusicPostId = null;
    });
  },
  onCollectionTap: function(event) {
    var postsCollected = wx.getStorageSync("posts_collected");
    var postCollected = postsCollected[this.data.postData.postId];
    this.showToast(postsCollected, postCollected);
    // this.showModal(postsCollected, postCollected);
  },
  showToast: function(postsCollected, postCollected) {
    // 取反操作（收藏变为未收藏，未收藏变为收藏）
    postCollected = !postCollected;
    postsCollected[this.data.postData.postId] = postCollected;
    // 更新收藏状态，缓存值
    wx.setStorageSync("posts_collected", postsCollected);
    // 更新数据绑定变量，从而实现切换图片                                          
    this.setData({
      collected: postCollected
    })
    // 收藏消息提示                                                                                                                                        
    wx.showToast({
      // 三元表达式
      title: postCollected ? "收藏成功" : "取消收藏",
      duration: 1000,
      icon: "success"
    })

    // showModal: function(postsCollected, postCollected) {
    //   var that = this;
    //   wx.showModal({
    //     title: '收藏',
    //     content: postCollected ? "取消收藏该文章" : "收藏该文章？",
    //     showCancel: "true",
    //     cancelText: "取消",
    //     cancelColor: "#333",
    //     confirmText: "确认",
    //     confirmColor: "#405f80",
    //     success: function(res) {
    //       if (res.confirm) {
    //         // 取反操作（收藏变为未收藏，未收藏变为收藏）
    //         postCollected = !postCollected;
    //         postsCollected[that.data.postData.postId] = postCollected;
    //         // 更新收藏状态，缓存值
    //         wx.setStorageSync("posts_collected", postsCollected);
    //         // 更新数据绑定变量，从而实现切换图片
    //         that.setData({
    //           collected: postCollected
    //         })
    //       }
    //     }
    //   })
    // }
  },
  onShareTap: function(event) {
    // wx.showActionSheet分享api
    var itemList = [
      "分享给微信好友",
      "分享到朋友圈",
      "分享给QQ好友",
      "分享到QQ空间",
      "分享到微博"
    ];
    wx.showActionSheet({
      itemList: itemList,
      itemColor: "#405f80",
      success: function(res) {
        // res.cancel 用户是否点击了取消按钮
        // res.tapIndex 数组元素的序号，从0开始
        wx.showModal({
          title: "用户" + itemList[res.tapIndex]
        })
      }
    })
  },
  onMusicTap: function(event) {
    // 音乐播放开始暂停
    var isPlay = this.data.isPlay;
    var postId = this.data.postData.postId;
    var music = postsData.postList[postId].music;
    console.log(isPlay);
    if (isPlay) {
      wx.pauseBackgroundAudio();
      this.setData({
        isPlay: false
      })
    } else {
      wx.playBackgroundAudio({
        dataUrl: music.url,
        title: music.title,
        coverImgUrl: music.coverImg
      })
      this.setData({
        isPlay: true
      })
    }
  }

})